#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/4/14 14:42
# @Author : 闫峰
# @File    : count_time.py
import os
import time
from functools import wraps

from  util.logging_tool.log_control import ERROR


def timer_decorator(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        start_time = time.time()
        result = func(*args, **kwargs)
        end_time = time.time()
        print(f"{func.__name__} took {end_time - start_time:.2f} seconds to run.")
        # print(f"{func.__name__} took {end_time - start_time:.2f} seconds to run.")
        return result
    return wrapper



def remove_log(dir_path):
    def decorator(func):
        @wraps(func)
        def swapper(*args, **kwargs):
            file_names = os.listdir(dir_path) # 获取目录下的文件列表
            # 删除以.log结尾的文件
            for file_name in file_names:
                if file_name.endswith('.log'):
                    file_path = os.path.join(dir_path, file_name)  # 拼接文件路径
                    if os.path.exists(file_path):  # 检查文件是否存在
                        os.remove(file_path)

            func(*args, **kwargs)
        return swapper
    return decorator


def remove_dir_log(dir_path):


    file_names = os.listdir(dir_path)  # 获取目录下的文件列表

    # 删除以.log结尾的文件
    for file_name in file_names:
        if file_name.endswith('.log'):
            file_path = os.path.join(dir_path, file_name)  # 拼接文件路径
            if os.path.exists(file_path):  # 检查文件是否存在
                os.remove(file_path)  # 删除文件
